<template>
  <div class="modern-stats-panel">
    <!-- 数据网格 -->
    <div class="stats-grid">
      <div 
        class="stat-card" 
        v-for="(item, index) in stats" 
        :key="index"
        :class="`card-${index + 1}`"
      >
        <div class="card-icon">
          <div class="icon-wrapper">
            <i :class="getIconClass(index)"></i>
          </div>
        </div>
        <div class="card-content">
          <div class="stat-value">{{ item.value }}</div>
          <div class="stat-label">{{ item.label }}</div>
        </div>
      </div>
    </div>

    <!-- 安全指数环形图 -->
    <div class="security-gauge" v-if="securityScore !== null">
      <div class="gauge-container">
        <div class="gauge-circle">
          <svg width="100" height="100">
            <circle
              cx="50"
              cy="50"
              r="45"
              fill="none"
              stroke="rgba(255, 255, 255, 0.1)"
              stroke-width="6"
            />
            <circle
              cx="50"
              cy="50"
              r="45"
              fill="none"
              stroke="#4cc9f0"
              stroke-width="6"
              stroke-linecap="round"
              :stroke-dasharray="`${securityScore * 2.83} 283`"
              class="gauge-fill"
            />
          </svg>
          <div class="gauge-center">
            <span class="gauge-value">{{ securityScore }}</span>
            <span class="gauge-label">安全指数</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import dashboardApi from '@/api/dashboard.js';

const stats = ref([
  { label: '扫描总数', value: '0', unit: '台' },
  { label: '支持DNSSEC', value: '0', unit: '台' },
  { label: '支持DoT', value: '0', unit: '台' },
  { label: '支持DoH', value: '0', unit: '台' }
]);

const securityScore = ref(null);
const loading = ref(false);

const getIconClass = (index) => {
  const icons = [
    'icon-server',
    'icon-shield',
    'icon-dot',
    'icon-doh'
  ];
  return icons[index] || 'icon-default';
};

const initData = async () => {
  try {
    loading.value = true;
    
    // 调用后端API获取仪表盘详情数据
    const response = await dashboardApi.getDashboardDetail();
    
    if (response.code === 200 && response.data) {
      const data = response.data;
      
      stats.value = [
        { label: '扫描总数', value: data.total ? data.total.toLocaleString() : '0', unit: '台' },
        { label: '支持DNSSEC', value: data.dnssec ? data.dnssec.toLocaleString() : '0', unit: '台' },
        { label: '支持DoT', value: data.dot ? data.dot.toLocaleString() : '0', unit: '台' },
        { label: '支持DoH', value: data.doh ? data.doh.toLocaleString() : '0', unit: '台' }
      ];

      securityScore.value = data.security || 0;
    } else {
      console.warn('获取数据失败:', response.message);
      // 使用默认值
      securityScore.value = 0;
    }
  } catch (error) {
    console.error('获取数据失败:', error);
    
    // 如果是401错误，不显示错误消息，让响应拦截器处理跳转
    if (error.code === 401) {
      console.log('Token过期，响应拦截器将处理跳转');
      return;
    }
    
    // 发生其他错误时使用默认值
    securityScore.value = 0;
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  initData();
});
</script>

<style scoped>
.modern-stats-panel {
  width: 100%;
  height: 100%;
  padding: 0;
  background: transparent;
}

/* 头部样式 */
.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 15px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.title-wrapper {
  display: flex;
  align-items: center;
  gap: 12px;
}

.title-icon {
  position: relative;
  width: 24px;
  height: 24px;
}

.pulse-dot {
  width: 12px;
  height: 12px;
  background: #4cc9f0;
  border-radius: 50%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation: pulse 2s infinite;
}

.pulse-dot::before {
  content: '';
  position: absolute;
  top: -6px;
  left: -6px;
  right: -6px;
  bottom: -6px;
  border: 2px solid #4cc9f0;
  border-radius: 50%;
  animation: pulse-ring 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

@keyframes pulse-ring {
  0% { transform: scale(0.5); opacity: 0; }
  50% { transform: scale(1); opacity: 0.3; }
  100% { transform: scale(1.5); opacity: 0; }
}

.panel-header h3 {
  margin: 0;
  font-size: 1.1em;
  font-weight: 600;
  color: white;
}

.live-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85em;
  color: rgba(255, 255, 255, 0.7);
}

.live-dot {
  width: 8px;
  height: 8px;
  background: #4ade80;
  border-radius: 50%;
  animation: blink 1.5s infinite;
}

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0.3; }
}

/* 数据网格 */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  height: 100%;
}

.stat-card {
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  padding: 16px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 8px;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.stat-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--card-color, #4cc9f0);
  opacity: 0.8;
}

.card-1 { --card-color: #4cc9f0; }
.card-2 { --card-color: #4895ef; }
.card-3 { --card-color: #4361ee; }
.card-4 { --card-color: #3f37c9; }

.stat-card:hover {
  transform: translateY(-2px);
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.25);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.card-icon {
  align-self: center;
  margin-bottom: 8px;
}

.icon-wrapper {
  width: 28px;
  height: 28px;
  background: var(--card-color, #4cc9f0);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.9;
}

.icon-wrapper i {
  font-size: 16px;
  color: white;
}

/* 图标样式 */
.icon-server::before { content: '🖥️'; }
.icon-shield::before { content: '🛡️'; }
.icon-dot::before { content: '🔐'; }
.icon-doh::before { content: '🌐'; }

.card-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-value {
  font-size: 1.4em;
  font-weight: 700;
  color: white;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 0.75em;
  color: rgba(255, 255, 255, 0.7);
  line-height: 1.2;
}

/* 安全指数环形图 */
.security-gauge {
  margin-top: 10px;
}

.gauge-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 120px;
}

.gauge-circle {
  position: relative;
  width: 100px;
  height: 100px;
}

.gauge-circle svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.gauge-fill {
  fill: none;
  stroke: #4cc9f0;
  stroke-width: 6;
  stroke-linecap: round;
  stroke-dasharray: 0 283;
  transition: stroke-dasharray 0.8s ease-in-out;
  filter: drop-shadow(0 0 6px rgba(76, 201, 240, 0.5));
}

.gauge-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.gauge-value {
  display: block;
  font-size: 1.5em;
  font-weight: 700;
  color: white;
}

.gauge-label {
  display: block;
  font-size: 0.7em;
  color: rgba(255, 255, 255, 0.7);
  margin-top: 2px;
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .stats-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  
  .stat-card {
    flex-direction: row;
    align-items: center;
    padding: 12px;
  }
  
  .card-content {
    margin-left: 12px;
  }
  
  .stat-value {
    font-size: 1.3em;
  }
}
</style>